package Exam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Login {
    private static PreparedStatement prepareStatement;
    private static Connection connection;
    static Scanner in = new Scanner(System.in);
    private static ResultSet set;
    public static void main(String[] args) throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/supermaket?useSSL=false&useUnicode=true&characterEncoding=utf-8", "root", "123456");
        System.out.println("欢迎使用超市收银系统：");
        System.out.println("请选择：");
        System.out.println("1.员工登录");
        System.out.println("2.会员登录（VIP）");
        int choice = in.nextInt();
        switch (choice) {
            case 1:
                System.out.println("请选择员工身份：");
                System.out.println("1.管理员");
                System.out.println("2.收银员");
                System.out.println("3.采购员");
                int role = in.nextInt();
                switch (role) {
                    case 1:
                        adminLogin();
                        break;
                    case 2:
                        cashierLogin();
                        break;
                    case 3:
                        buyerLogin();
                        break;
                    default:
                        break;
                }
                break;
            case 2:

                break;
            default:
                break;
        }
    }
    private static void buyerLogin() {
        System.out.println("采购员登录界面：");
        System.out.println("请输入工号：");
        String number = in.nextLine();
        System.out.println("请输入密码：");
        String pswd = in.nextLine();
        try {
            login(3,number,pswd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    private static void cashierLogin() {
        System.out.println("收银员登录界面：");
        System.out.println("请输入工号：");
        String number = in.nextLine();
        System.out.println("请输入密码：");
        String pswd = in.nextLine();
        try {
            login(2,number,pswd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void adminLogin() {
        System.out.println("管理员登录界面：");
        System.out.println("请输入工号：");
        String number = in.nextLine();
        System.out.println("请输入密码：");
        String pswd = in.nextLine();
        try {
            login(1,number,pswd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int adminChoice = in.nextInt();
        switch (adminChoice) {
            case 4:
                System.out.println("1.天营业额");
                System.out.println("2.月营业额");
                System.out.println("3.季度营业额");
                System.out.println("4.年营业额");
                break;

            default:
                break;
        }
    }
    private static void login(int role,String number, String pswd) throws SQLException {

        String sql = "select * from employee where number=? and password=? and role=?";
        prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, number);
        prepareStatement.setString(2, pswd);
        prepareStatement.setInt(3, role);
        set = prepareStatement.executeQuery();
        if(set.next()) {
            System.out.println("登录成功！");
            switch (role) {
                case 1:
                    System.out.println("请选择：");
                    System.out.println("1.收银员账户管理");
                    System.out.println("2.采购员账户管理");
                    System.out.println("3.员工出勤管理");
                    System.out.println("4.查询超市的营业额");
                    System.out.println("5.会员管理");
                    System.out.println("6.查询所有人员信息");
                    System.out.println("7.打卡上班");
                    System.out.println("8.打卡下班");
                    int function = in.nextInt();
                    switch (function) {
                        case 7:
                            clickIn(number);
                            break;
                        case 8:
                            clickOff(number);
                            break;
                        default:
                            break;
                    }
                    break;
                case 2:
                    System.out.println("请选择：");
                    System.out.println("1.收银结算");
                    System.out.println("2.会员积分查询");
                    System.out.println("3.开通会员");
                    System.out.println("4.打卡上班");
                    System.out.println("5.打卡下班");
                    int function1 = in.nextInt();
                    switch (function1) {
                        case 1:
                            clickIn(number);
                            break;
                        case 4:
                            clickIn(number);
                            break;
                        case 5:
                            clickOff(number);
                            break;
                        default:
                            break;
                    }
                    break;
                case 3:
                    System.out.println("请选择：");
                    System.out.println("1.进行商品补货");
                    System.out.println("2.查询进货单");
                    System.out.println("3.打卡上班");
                    System.out.println("4.打卡下班");
                    int function2 = in.nextInt();
                    switch (function2) {
                        case 3:
                            clickIn(number);
                            break;
                        case 4:
                            clickOff(number);
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }else {
            System.out.println("登录失败");
            switch (role) {
                case 1:
                    adminLogin();
                    break;
                case 2:
                    cashierLogin();
                    break;
                case 3:
                    buyerLogin();
                    break;
                default:
                    break;
            }
        }
    }
    private static void clickOff(String number) throws SQLException {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = df.format(date);
        Timestamp time  = new Timestamp(date.getTime());
        String sql = "update clock_info set clock_off_time=? where employee_no = ?";
        prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setTimestamp(1, time);
        prepareStatement.setString(2, number);
        prepareStatement.executeUpdate();
        System.out.println("下班打卡成功！");
    }
    private static void clickIn(String number) throws SQLException {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = df.format(date);
        Timestamp time  = new Timestamp(date.getTime());
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        String sql = "INSERT INTO clock_info(employee_no,clock_in_time,clock_date) VALUES (?,?,?)";
        prepareStatement = connection.prepareStatement(sql);
        prepareStatement.setString(1, number);
        prepareStatement.setTimestamp(2, time);
        prepareStatement.setDate(3, date1);
        prepareStatement.executeUpdate();
        System.out.println("上班打卡成功！");

    }

}